/**
 * @version 2007-1-6
 * @author xalinx at gmail dot com
 */
package org.nanhill.webflow.spring;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nanhill.commons.pattern.flow.FlowFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @version 2007-1-6
 * @author xalinx at gmail dot com
 * 
 */
public abstract class AbstractFlowFactoryBean implements FactoryBean,
		InitializingBean, BeanFactoryAware {

	/** Logger available to subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	/** sigleton flow factory */
	private FlowFactory flowFactory;

	private BeanFactory beanFactory;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.FactoryBean#getObject()
	 */
	public Object getObject() throws Exception {
		return this.flowFactory;
	}

	public Class getObjectType() {
		return (this.flowFactory != null) ? this.flowFactory.getClass()
				: FlowFactory.class;
	}

	public boolean isSingleton() {
		return true;
	}

	public void afterPropertiesSet() throws Exception {
		this.flowFactory = buildFlowFactory();
	}

	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

	protected Object getActionTarget(String beanName) {
		return this.beanFactory.getBean(beanName);
	}

	protected abstract FlowFactory buildFlowFactory() throws Exception;

}
